home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Save PICT file / SavePictFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  6.2 KB  |  225 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        SavePictFile.c
  3.  
  4.     Contains:    Save a PICT file:  Creates a QuickDraw PICT and saves it as a PICT file,
  5.                 including the required header of 512 bytes of nothing important.
  6.  
  7.  
  8.     Written by:     
  9.  
  10.     Copyright:    Copyright © 1984-1999 by Apple Computer, Inc., All Rights Reserved.
  11.  
  12.                 You may incorporate this Apple sample source code into your program(s) without
  13.                 restriction. This Apple sample source code has been provided "AS IS" and the
  14.                 responsibility for its operation is yours. You are not permitted to redistribute
  15.                 this Apple sample source code as "Apple sample source code" after having made
  16.                 changes. If you're going to re-distribute the source, we require that you make
  17.                 it clear in the source that the code was descended from Apple sample source
  18.                 code, but that you've made changes.
  19.  
  20.     Change History (most recent first):
  21.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  22.                                             for demonstration purposes.
  23.                 7/14/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */ 
  27. #include "CarbonPrefix.h"
  28. #include <Files.h>
  29. //#include <StandardFile.h>
  30. #include <Fonts.h>
  31. #include <Quickdraw.h>
  32. #include <Navigation.h>
  33. #include <Script.h>
  34.  
  35.  
  36.  
  37. PicHandle InitPicture(void);
  38. void PutPictToFile(PicHandle thePicture);
  39.  
  40. /*------ main ----------------------------------------------------------------------------*/
  41.  
  42. PicHandle ourPict;
  43.  
  44. void main()
  45. {
  46.     
  47.     // Most initialized functions are not needed anymore, InitCursor being
  48.     // the exception
  49.  
  50.     //InitGraf(&qd.thePort);
  51.     //InitFonts();
  52.     //InitWindows();
  53.     //InitMenus();
  54.     //TEInit();
  55.     //InitDialogs(nil);
  56.     InitCursor();
  57.     
  58.     // Not available in carbon
  59.     //OpenPort(&myPort);
  60.     
  61.       ourPict = InitPicture();                       // get our picture
  62.       HLock((Handle)ourPict);
  63.       PutPictToFile(ourPict);                        // put it to a file
  64.       HUnlock((Handle)ourPict);
  65.       KillPicture(ourPict);                       // and then kill it
  66.  
  67. } /* main */
  68.  
  69.  
  70.  
  71. /*------ PutPictToFile ----------------------------------------------------------------------------*/
  72.  
  73.  
  74. void PutPictToFile(PicHandle thePicture)
  75.  
  76. {
  77.     /*
  78.     SFReply    tr;
  79.     short    fRefNum, count;
  80.     long inOutCount;
  81.     Point    where;
  82.     unsigned char header[512];
  83.     OSErr myError;
  84.     
  85.     for (count = 0; count < 512; count++)
  86.         header[count] = 0x00;
  87.     
  88.     where.h=100; where.v=50;     // where the  Standard File dialog window goes
  89.     
  90.     SFPutFile( where, "\pSave PICT as:", "\pMy PICT File", NULL, &tr );
  91.     
  92.     if ( tr.good ) {
  93.         myError = Create(tr.fName, tr.vRefNum, '????', 'PICT');
  94.         if (myError == dupFNErr) {
  95.             myError = FSDelete(tr.fName,tr.vRefNum);
  96.             myError = Create(tr.fName, tr.vRefNum, '????', 'PICT');
  97.         }        // this is quick 'n' dirty or there'd be more robust handling here 
  98.         
  99.         myError = FSOpen( tr.fName, tr.vRefNum, &fRefNum );
  100.         if ( myError == noErr ) { 
  101.             inOutCount = 512;
  102.             myError = FSWrite(fRefNum, &inOutCount, header);        // write the header
  103.             HLock((Handle)thePicture);
  104.             if (myError == noErr) {                    // don't write if error the first time
  105.                 inOutCount = GetHandleSize((Handle)thePicture);
  106.                 myError = FSWrite(fRefNum,&inOutCount,*thePicture);
  107.             }
  108.             FSClose( fRefNum );            // close it 
  109.             HUnlock((Handle)thePicture);
  110.         }
  111.     }
  112.     */
  113.     
  114.     // The file services API in Carbon is called Navigation Services.
  115.     // All (or nearly all) of the Standard File routines are not supported in Carbon.
  116.     // Note:  not checking for every error, keeping the example simple
  117.     // Code adapted from: http://developer.apple.com/techpubs/carbon/Files/NavigationServices/Navigation_Services/Conceptual/NavServices-15.html#elementId-1012312
  118.     
  119.     OSErr               anErr = noErr;
  120.     NavReplyRecord      reply;
  121.     NavDialogOptions    dialogOptions;
  122.     OSType              fileTypeToSave = 'PICT';
  123.     OSType              creatorType = 'ogle';
  124.     AEKeyword           theKeyword;
  125.     DescType            actualType;
  126.     Size                actualSize;
  127.     FSSpec              documentFSSpec;
  128.     long                inOutCount;
  129.     short                refNum, count;
  130.     unsigned char         header[512];
  131.     
  132.     for (count = 0; count < 512; count++)
  133.         header[count] = 0x00;
  134.  
  135.     anErr = NavGetDefaultDialogOptions(&dialogOptions); 
  136.     dialogOptions.dialogOptionFlags |= kNavSelectDefaultLocation;
  137.     
  138.     anErr = NavPutFile( nil, 
  139.                         &reply, 
  140.                         &dialogOptions, 
  141.                         nil,
  142.                         fileTypeToSave, 
  143.                         creatorType, 
  144.                         nil );
  145.     
  146.     if (anErr == noErr && reply.validRecord) {
  147.     
  148.                         
  149.         anErr = AEGetNthPtr(&(reply.selection), 1, typeFSS,
  150.                                 &theKeyword, &actualType,
  151.                                 &documentFSSpec, sizeof(documentFSSpec),
  152.                                 &actualSize );
  153.                                 
  154.         if (anErr == noErr) {
  155.         
  156.                 anErr = FSpCreate(&documentFSSpec, creatorType, fileTypeToSave, smSystemScript);
  157.             if (anErr == dupFNErr) {
  158.                 anErr = FSpDelete(&documentFSSpec);
  159.                 anErr = FSpCreate(&documentFSSpec, creatorType, fileTypeToSave, smSystemScript);
  160.             }        // this is quick 'n' dirty or there'd be more robust handling here
  161.             
  162.             // write the file
  163.             FSpOpenDF(&documentFSSpec, fsRdWrPerm, &refNum );
  164.             inOutCount = 512;
  165.                anErr = FSWrite(refNum, &inOutCount, header);        // write the header
  166.             if (anErr == noErr) {
  167.                 inOutCount = GetHandleSize((Handle)thePicture);
  168.                 anErr = FSWrite(refNum,&inOutCount,*thePicture);
  169.             }
  170.             FSClose( refNum );
  171.         }
  172.         reply.translationNeeded = false;
  173.         anErr = NavCompleteSave(&reply, kNavTranslateInPlace);
  174.     
  175.        NavDisposeReply(&reply);
  176.     }
  177. }
  178.  
  179.  
  180.  
  181. /*------ InitPicture ----------------------------------------------------------------------*/
  182.  
  183. PicHandle InitPicture (void)
  184.  
  185. {
  186.     Rect myRect;
  187.     PicHandle thePicHandle;
  188.     //CGrafPort myPort;
  189.     CGrafPtr myPort;
  190.     PixPatHandle thePixPat;
  191.     short theFont, textSize = 14;
  192.     
  193.     // Not available in carbon
  194.     //OpenCPort(&myPort);
  195.     myPort = CreateNewPort();
  196.     SetRect(&myRect,0,0,200,200);
  197.     thePicHandle = OpenPicture(&myRect);
  198.     ClipRect(&myRect);
  199.     
  200.     thePixPat = GetPixPat(128);
  201.     
  202.     FillCOval(&myRect,thePixPat);
  203.     
  204.     MoveTo(22,22);
  205.     LineTo(55,55);
  206.     LineTo(58,22);
  207.     LineTo(22,58);
  208.     
  209.     // Not recommended in Carbon
  210.     //GetFNum ("\pTimes", &theFont);
  211.     theFont = FMGetFontFamilyFromName("\pTimes");
  212.     
  213.     TextFont (theFont);
  214.     TextSize (textSize);
  215.     
  216.     DrawString("\pA wonderful test");
  217.     
  218.     ClosePicture();
  219.     //CloseCPort(&myPort);
  220.     DisposePort(myPort);
  221.     
  222.     return(thePicHandle);
  223.         
  224. }  /* InitPicture */
  225.